<?php
class ChitietnhapsController extends AppController {

	var $name = 'Chitietnhaps';
	var $uses = array('Chitietnhap','Nhanvien','Cuonsach','Dausach');

	function index() {
		$this->Chitietnhap->recursive = 0;
		$this->set('chitietnhaps', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid chitietnhap', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('chitietnhap', $this->Chitietnhap->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Chitietnhap->create();
			if ($this->Chitietnhap->save($this->data)) {
				$this->Session->setFlash(__('The chitietnhap has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The chitietnhap could not be saved. Please, try again.', true));
			}
		}
		$hoadonnhaps = $this->Chitietnhap->Hoadonnhap->find('list');
		$dausaches = $this->Chitietnhap->Dausach->find('list');
		$this->set(compact('hoadonnhaps', 'dausaches'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid chitietnhap', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Chitietnhap->save($this->data)) {
				$this->Session->setFlash(__('The chitietnhap has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The chitietnhap could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Chitietnhap->read(null, $id);
		}
		$hoadonnhaps = $this->Chitietnhap->Hoadonnhap->find('list');
		$dausaches = $this->Chitietnhap->Dausach->find('list');
		$this->set(compact('hoadonnhaps', 'dausaches'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for chitietnhap', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Chitietnhap->delete($id)) {
			$this->Session->setFlash(__('Chitietnhap deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Chitietnhap was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
	
	function dausachnhap($id=null)
	{
		if(!$id)
		{
			$this->Session->setFlash(__('Hóa đơn không tồn tại', true));
			$this->redirect(array('controller'=>'hoadonnhaps','action'=>'dshoadon'));
		}
		if(!empty($this->data))
		{
			$this->data['Chitietnhap']['hoadonnhap_id'] = $id;
			$this->data['Chitietnhap']['dongia']=str_replace('.','',$this->data['Chitietnhap']['dongia']);
			$soluong = $this->data['Chitietnhap']['soluong'];
			$socuon = $this->data['Chitietnhap']['socuon'];
			$cuonsaches = $this->Cuonsach->find('list',array('fields'=>array('macs'),'conditions'=>array('dausach_id'=>$this->data['Chitietnhap']['dausach_id'])));
			$dausaches = $this->Dausach->read('',$this->data['Chitietnhap']['dausach_id']);
			$khongmuon = 0;
			$stt = 1;
			$this->Chitietnhap->create();
			if($this->Chitietnhap->save($this->data))
			{
				for($i=0; $i<$soluong; $i++ )
				{
					
					$macs = $dausaches['Dausach']['mads'].'.'.$stt;
					while(in_array('MON'.$macs,$cuonsaches)|| in_array('MOL'.$macs,$cuonsaches))
					{
						$stt++;
						if($khongmuon<$socuon)
						{
							$macs = $dausaches['Dausach']['mads'].'.'.$stt;
						}
						else
						{
							$macs = $dausaches['Dausach']['mads'].'.'.$stt;
						}
					}
					if($khongmuon<$socuon)
					{
						$macs = 'MON'.$macs;
						$khongmuon++;
					}
					else
					{
						$macs = 'MOL'.$macs;
					}
					$stt++;
					$record['Cuonsach']['macs']=$macs;
					$record['Cuonsach']['dausach_id']=$this->data['Chitietnhap']['dausach_id'];
					$this->Cuonsach->create();
					if(!$this->Cuonsach->saveAll($record))
					{
						$this->Session->setFlash(__('Tạo cuốn sách của đầu sách không thành công. Xin vui lòng thử lại', true));
					}
				}
			}
			else
			{
				$this->Session->setFlash(__('Lưu không thành công. Xin vui lòng thử lại', true));
			}
			$this->redirect(array('action'=>'dausachnhap',$id));
		}
		$this->data = $this->Chitietnhap->Hoadonnhap->read(null,$id);
		$chitietnhaps = $this->Chitietnhap->find('all',array('recursive'=>0,'conditions'=>array('hoadonnhap_id'=>$id)));
		$this->Chitietnhap->Dausach->virtualFields= array('dausach'=>'Concat(mads," - ",tensach)');
		$this->Chitietnhap->Dausach->displayField='dausach';
		$dausaches = $this->Chitietnhap->Dausach->find('list');
		$this->set(compact('chitietnhaps','dausaches','id'));
	}

	function xemchitiethoadon($id=null)
	{
		if(!$id)
		{
			$this->Session->setFlash(__('Hóa đơn không tồn tại', true));
			$this->redirect(array('controller'=>'hoadonnhaps','action'=>'dshoadon'));
		}
		$this->data = $this->Chitietnhap->Hoadonnhap->read(null,$id);
		$chitietnhaps = $this->Chitietnhap->find('all',array('recursive'=>0,'conditions'=>array('hoadonnhap_id'=>$id)));
		$this->set(compact('chitietnhaps','id'));
	}
}
